package com.ikkong.biz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ikkong.biz.BizCst;
import com.ikkong.biz.meta.intercept.BizNoticeIntercept;
import com.ikkong.biz.meta.intercept.BizReceiveNoticeIntercept;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import com.ikkong.core.toolbox.Record;
import com.ikkong.system.controller.base.AdminBaseController;
import com.ikkong.biz.model.Notice;
import com.ikkong.biz.model.NoticeResult;
import com.ikkong.biz.service.NoticeService;
import com.ikkong.biz.service.impl.NoticeServiceImpl;

/**
 * Generated by Blade.
 * 2016-11-21 18:00:39
 */
public class BizNoticeController extends AdminBaseController {
	private static String CODE = "bizNotice";
	private static String PERFIX = "tb_notice";
	private static String LIST_SOURCE = "BizNotice.list";
	private static String BASE_PATH = "/biz/notice/";
	
	NoticeService service = new NoticeServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "notice.html");
	}

	public void receive() {
		setAttr("code", CODE);
		render(BASE_PATH + "receive_notice.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "notice_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Notice notice = service.findById(id);
		setAttr("model", JsonKit.toJson(notice));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "notice_edit.html");
	}

	public void view() {
		String id = getPara(0);
		Notice notice = service.findById(id);
        setAttr("item",notice);
		setAttr("model", JsonKit.toJson(notice));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "notice_view.html");
	}

	public void receive_view() {
		String id = getPara(0);
        Notice notice = service.findById(id);
        Map<String,Object> modelOrMap = new HashMap<String, Object>();
        modelOrMap.put("notice_id", id);
        modelOrMap.put("customer_id", ShiroKit.getTeacher().getId());
        Blade.create(NoticeResult.class).updateBy("is_read=1", "notice_id=#{notice_id} and customer_id=#{customer_id}", modelOrMap);
        setAttr("item",notice);
        setAttr("model", JsonKit.toJson(notice));
		setAttr("id", id);
		setAttr("auto_close",true);
		setAttr("code", CODE);
		render(BASE_PATH + "receive_notice_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE,new BizNoticeIntercept());
		renderJson(grid);
	}
	public void receive_list() {
		Object grid = paginate("BizNotice.receive_list",new BizReceiveNoticeIntercept());
		renderJson(grid);
	}

    public void getStuTree(){
        List<Record> list = service.getStuNoticeTree();
        renderJson(json(list));
    }

    public void getParTree(){
        List<Record> list = service.getParNoticeTree();
        renderJson(json(list));
    }

    public void getTeaTree(){
        List<Record> list = service.getTeaNoticeTree();
        renderJson(json(list));
    }

	public void save() {
		Notice notice = mapping(PERFIX, Notice.class);

        boolean temp = service.doSend(notice);

		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Notice notice = mapping(PERFIX, Notice.class);
		notice.setVersion(notice.getVersion()+1);
		boolean temp = service.update(notice);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
	
	public void unRead(){
		int count = service.getUnRead();
		renderJson(json(count));
	}
}
